#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QThread>
#include <QMovie>
#include <QSerialPort>
#include <QTime>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground);

   QPixmap originalPixmap(":/resource/arrow.png");
   QPixmap flippedUp = originalPixmap.transformed(QTransform().rotate(90));
   QPixmap flippedDown = originalPixmap.transformed(QTransform().rotate(-90));
   QPixmap flippedLeft = originalPixmap.transformed(QTransform().rotate(180));

   ui->but_right->setIcon(originalPixmap);
   ui->but_up->setIcon(flippedDown);
   ui->but_down->setIcon(flippedUp);
   ui->but_left->setIcon(flippedLeft);

   ui->but_open->setCheckable(true);
   pool_port=QSerialPortInfo::availablePorts();

   QList<QSerialPortInfo>::const_iterator it;

   for(it=pool_port.begin();it!=pool_port.end();it++){
       ui->comboBox->addItem((*it).portName());
   }

   port=new QSerialPort;
   connect(port, SIGNAL(readyRead()), this, SLOT(setText()));

   timer=new QTimer();
   timer->setInterval(20);
   connect(timer, SIGNAL(timeout()), this, SLOT(change()));
   timer->start();

   QMovie *mv=new QMovie(":/resource/hehe.gif");
   ui->gif->setMovie(mv);
   mv->start();

   ui->dial_rotate->setWrapping(false);
}

Widget::~Widget()
{
    delete ui;
    delete port;
    delete timer;
}

void Widget::change()
{
    if(!is_open)return;
    
    if(is_left){
        qDebug()<<"timer left"<<m4_theta;
        if(m4_theta>0)m4_theta-=5;

        ui->textEdit->append("left to:"+QString::number(m4_theta));

        sendMessage(m4_theta,0);
    }else if(is_right){
        qDebug()<<"timer right"<<m4_theta;
        if(m4_theta<180)m4_theta+=5;

        ui->textEdit->append("right to:"+QString::number(m4_theta));

        sendMessage(m4_theta,0);
    }else if(is_up){
        qDebug()<<"timer up";
        if(m6_theta<180)m6_theta+=5;

        ui->textEdit->append("up to:"+QString::number(m6_theta));

        sendMessage(m6_theta,2);

    }else if(is_down){
        qDebug()<<"timer down";
        if(m6_theta>0)m6_theta-=5;

        ui->textEdit->append("down to:"+QString::number(m6_theta));

        sendMessage(m6_theta,2);
    }else if(is_front){
        qDebug()<<"timer front";
        if(m5_theta>0)m5_theta-=5;

        ui->textEdit->append("front to:"+QString::number(m5_theta));

        sendMessage(m5_theta,1);
    }else if(is_after){
        qDebug()<<"timer after";
        if(m5_theta<180)m5_theta+=5;

        ui->textEdit->append("after to:"+QString::number(m5_theta));

        sendMessage(m5_theta,1);
    }
    on_but_reset_clicked();

}

void Widget::setText()
{
    QString str=port->readAll();
    qDebug()<<str;
    ui->textEdit->append("add :"+str);
}

void Widget::on_but_open_clicked(bool checked)
{
    QString band=ui->line_rute->text();
    QString data=ui->line_data->text();
    QString flaw=ui->line_control->text();
    QString check=ui->line_check->text();
    QString end=ui->line_end->text();

    // 设置波特率
    if(band == "115200"){
        port->setBaudRate(QSerialPort::Baud115200);
    } else {
        // 其他波特率设置或默认值
        port->setBaudRate(QSerialPort::Baud9600); // 例如，默认设置为9600
    }

    // 设置数据位
    if(band == "8"){
        port->setDataBits(QSerialPort::Data8);
    } else {
        // 其他数据位设置或默认值
        port->setDataBits(QSerialPort::Data8); // 默认设置为8位数据位
    }

    // 设置流控制
    if(band == "0"){
        port->setFlowControl(QSerialPort::NoFlowControl);
    } else {
        // 其他流控制设置或默认值
        port->setFlowControl(QSerialPort::NoFlowControl); // 默认不使用流控制
    }

    // 设置校验位
    if(band == "0"){
        port->setParity(QSerialPort::NoParity);
    } else {
        // 其他校验位设置或默认值
        port->setParity(QSerialPort::NoParity); // 默认不使用校验位
    }

    // 设置停止位
    if(band == "1"){
        port->setStopBits(QSerialPort::OneStop);
    } else {
        // 其他停止位设置或默认值
        port->setStopBits(QSerialPort::OneStop); // 默认设置为1个停止位
    }

    port->setPortName(ui->comboBox->currentText());

    qDebug()<<checked;
    if(checked){
        bool ret=port->open(QIODevice::ReadWrite);//读写模式打开串口
        if(ret){
            ui->textEdit->append("打开成功！");
            ui->but_open->setText("关闭");
        }
        else{
            ui->but_open->setChecked(false);
            ui->textEdit->append("打开失败！");
        }
    }else{
        port->close();//关闭串口
        ui->textEdit->append("关闭成功！");
        ui->but_open->setText("打开");
    }
    is_open=checked;
}

void Widget::on_but_up_pressed()
{
    is_up=true;
}

void Widget::on_but_left_pressed()
{
    is_left=true;
}

void Widget::on_but_down_pressed()
{
    is_down=true;
}

void Widget::on_but_right_pressed()
{
    is_right=true;
}

void Widget::delay(int msec) {
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while (QTime::currentTime() < dieTime) {
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    }
}

void Widget::on_but_reset_clicked()
{
    for(int i=135;i>45;i--){
        qDebug()<<"jump"<<i;
        sendMessage(i,0);
        delay(5);
        sendMessage(i,1);
        delay(5);
        sendMessage(i,2);
        delay(5);
        sendMessage(i,8);
        delay(5);
        sendMessage(45-i-135,3);
        delay(5);
    }
    delay(100);
    for(int i=45;i<135;i++){
        qDebug()<<"jump"<<i;
        sendMessage(i,0);
        delay(5);
        sendMessage(i,1);
        delay(5);
        sendMessage(i,2);
        delay(5);
        sendMessage(i,8);
        delay(5);
        sendMessage(135-i-45,3);
        delay(5);
    }
}

void Widget::on_but_up_released()
{
    is_up=false;
}

void Widget::on_but_left_released()
{
    is_left=false;
}

void Widget::on_but_right_released()
{
    is_right=false;
}


void Widget::on_but_down_released()
{
    is_down=false;
}

void Widget::on_Slider_hand_valueChanged(int value)
{
    qDebug()<<"hand"<<value;

    ui->textEdit->append("hand to:"+QString::number(value));

    sendMessage(value,5);
}

void Widget::on_but_after_pressed()
{
    is_after = true;
}

void Widget::on_but_front_pressed()
{
    is_front = true;
}

void Widget::on_but_front_released()
{
    is_front = false;
}

void Widget::on_but_after_released()
{
    is_after = false;
}

void Widget::on_dial_rotate_valueChanged(int value)
{
    qDebug()<<"rotate!";

    ui->textEdit->append("rotate to:"+QString::number(value));

    sendMessage(value,4);
}

void Widget::on_but_sned_clicked()
{
    QString str=ui->line_send->text();

    ui->textEdit->append("send to:"+str);

    if(!port->write(str.toLatin1())){
        qDebug()<<"串口传输失败！";
    }
}

bool Widget::sendMessage(int data, int id)
{
    unsigned char m[5]={0xff,0x02,0x00,0x00,0x00};

    int date_d=data*11.1111+500;

    m[2]=0x04+id;
    m[3]=date_d & 0xff;
    m[4]=date_d >> 8;

    if(!port->write((const char*)m,5)){
        qDebug()<<"串口传输失败！";
        return false;
    }
    return true;
}
